<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="Seata,Nacos,分布式事务" />
	<meta name="description" content="本文讲述如何使用Seata整合Nacos配置" />
	<!-- 网页标签标题 -->
	<title>Seata分布式事务启用Nacos做配置中心</title>
  <link rel="shortcut icon" href="/img/seata_logo_small.jpeg"/>
	<link rel="stylesheet" href="/build/blogDetail.css" />
</head>
<body>
	<div id="root"><div class="blog-detail-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/zh-cn/index.html"><img class="logo" src="/img/seata_logo.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">En</span><div class="header-menu"><img class="header-menu-toggle" src="/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/zh-cn/index.html" target="_self">首页</a></li><li class="menu-item menu-item-normal"><a href="/zh-cn/docs/overview/what-is-seata.html" target="_self">文档</a></li><li class="menu-item menu-item-normal"><a href="/zh-cn/docs/developers/developers_dev.html" target="_self">开发者</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/zh-cn/blog/index.html" target="_self">博客</a></li><li class="menu-item menu-item-normal"><a href="/zh-cn/community/index.html" target="_self">社区</a></li><li class="menu-item menu-item-normal"><a href="/zh-cn/blog/download.html" target="_self">下载</a></li></ul></div></div></header><section class="blog-content markdown-body"><h1>Seata分布式事务启用Nacos做配置中心</h1>
<p><a href="https://gitee.com/itCjb/springboot-dubbo-mybatisplus-seata">项目地址</a></p>
<p>本文作者：FUNKYE(陈健斌),杭州某互联网公司主程。</p>
<h1>前言</h1>
<p>上次发布了直连方式的seata配置,详细可以看这篇<a href="http://seata.io/zh-cn/blog/springboot-dubbo-mybatisplus-seata.html">博客</a></p>
<p>我们接着上一篇的基础上去配置nacos做配置中心跟dubbo注册中心.</p>
<h2>准备工作</h2>
<p>​	1.首先去nacos的github上下载<a href="https://github.com/alibaba/nacos/releases/tag/1.1.4">最新版本</a></p>
<p>​	<img src="/img/blog/20191202203649.png" alt=""></p>
<p>​	2.下载好了后,很简单,解压后到bin目录下去启动就好了,看到如图所示就成了：</p>
<p><img src="/img/blog/20191202203943.png" alt=""></p>
<p>​	3.启动完毕后访问:<a href="http://127.0.0.1:8848/nacos/#/login">http://127.0.0.1:8848/nacos/#/login</a></p>
<p><img src="/img/blog/20191202204101.png" alt=""></p>
<p>是不是看到这样的界面了?输入nacos(账号密码相同),先进去看看吧.</p>
<p>这时候可以发现没有任何服务注册</p>
<p><img src="/img/blog/20191202204147.png" alt="20191202204147"></p>
<p>别急我们马上让seata服务连接进来.</p>
<h2>Seata配置</h2>
<p>​	1.进入seata的conf文件夹看到这个木有?</p>
<p><img src="/img/blog/20191202204259.png" alt=""></p>
<p>就是它,编辑它:</p>
<p><img src="/img/blog/20191202204353.png" alt="20191202204353"></p>
<p><img src="/img/blog/20191202204437.png" alt="20191202204437"></p>
<p>​	2.然后记得保存哦!接着我们把registry.conf文件打开编辑：</p>
<pre><code>registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = &quot;nacos&quot;

  nacos {
    serverAddr = &quot;localhost&quot;
    namespace = &quot;&quot;
    cluster = &quot;default&quot;
  }
  eureka {
    serviceUrl = &quot;http://localhost:8761/eureka&quot;
    application = &quot;default&quot;
    weight = &quot;1&quot;
  }
  redis {
    serverAddr = &quot;localhost:6379&quot;
    db = &quot;0&quot;
  }
  zk {
    cluster = &quot;default&quot;
    serverAddr = &quot;127.0.0.1:2181&quot;
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = &quot;default&quot;
    serverAddr = &quot;127.0.0.1:8500&quot;
  }
  etcd3 {
    cluster = &quot;default&quot;
    serverAddr = &quot;http://localhost:2379&quot;
  }
  sofa {
    serverAddr = &quot;127.0.0.1:9603&quot;
    application = &quot;default&quot;
    region = &quot;DEFAULT_ZONE&quot;
    datacenter = &quot;DefaultDataCenter&quot;
    cluster = &quot;default&quot;
    group = &quot;SEATA_GROUP&quot;
    addressWaitTime = &quot;3000&quot;
  }
  file {
    name = &quot;file.conf&quot;
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = &quot;nacos&quot;

  nacos {
    serverAddr = &quot;localhost&quot;
    namespace = &quot;&quot;
  }
  consul {
    serverAddr = &quot;127.0.0.1:8500&quot;
  }
  apollo {
    app.id = &quot;seata-server&quot;
    apollo.meta = &quot;http://192.168.1.204:8801&quot;
  }
  zk {
    serverAddr = &quot;127.0.0.1:2181&quot;
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = &quot;http://localhost:2379&quot;
  }
  file {
    name = &quot;file.conf&quot;
  }
}

</code></pre>
<p>都编辑好了后，<a href="http://xn--nacos-config-2b7sf85sq33gseza.sh">我们运行nacos-config.sh</a>,这时候我们配置的nacos-config.txt的内容已经被发送到nacos中了详细如图：</p>
<p><img src="/img/blog/20191202205743.png" alt="20191202205743"></p>
<p>出现以上类似的代码就是说明成功了，接着我们登录nacos配置中心，查看配置列表，出现如图列表说明配置成功了：</p>
<p><img src="/img/blog/20191202205912.png" alt="20191202205912"></p>
<p>看到了吧,你的配置已经全部都提交上去了,如果再git工具内运行sh不行的话,试着把编辑sh文件,试试改成如下操作</p>
<pre><code class="language-shell">for line in $(cat nacos-config.txt)

do

key=${line%%=*}
value=${line#*=}
echo "\r\n set "${key}" = "${value}

result=`curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=$key&amp;group=SEATA_GROUP&amp;content=$value"`

if [ "$result"x == "true"x ]; then

  echo "\033[42;37m $result \033[0m"

else

  echo "\033[41;37 $result \033[0m"
  let error++

fi

done


if [ $error -eq 0 ]; then

echo  "\r\n\033[42;37m init nacos config finished, please start seata-server. \033[0m"

else

echo  "\r\n\033[41;33m init nacos config fail. \033[0m"

fi
</code></pre>
<p>​	3.目前我们的准备工作全部完成,我们去seata-service/bin去运行seata服务吧,如图所示就成功啦!</p>
<p><img src="/img/blog/20191202210112.png" alt="20191202210112"></p>
<h1>进行调试</h1>
<p>​	1.首先把springboot-dubbo-mybatsiplus-seata项目的pom的依赖更改,去除掉zk这些配置,因为我们使用nacos做注册中心了.</p>
<pre><code class="language-java">	&lt;properties&gt;
		&lt;webVersion&gt;3.1&lt;/webVersion&gt;
		&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
		&lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
		&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
		&lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
		&lt;HikariCP.version&gt;3.2.0&lt;/HikariCP.version&gt;
		&lt;mybatis-plus-boot-starter.version&gt;3.2.0&lt;/mybatis-plus-boot-starter.version&gt;
	&lt;/properties&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.1.8.RELEASE&lt;/version&gt;
	&lt;/parent&gt;
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.alibaba.nacos&lt;/groupId&gt;
			&lt;artifactId&gt;nacos-client&lt;/artifactId&gt;
			&lt;version&gt;1.1.4&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.apache.dubbo&lt;/groupId&gt;
			&lt;artifactId&gt;dubbo-registry-nacos&lt;/artifactId&gt;
			&lt;version&gt;2.7.4.1&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.apache.dubbo&lt;/groupId&gt;
			&lt;artifactId&gt;dubbo-spring-boot-starter&lt;/artifactId&gt;
			&lt;version&gt;2.7.4.1&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
			&lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
			&lt;artifactId&gt;fastjson&lt;/artifactId&gt;
			&lt;version&gt;1.2.60&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- &lt;dependency&gt; &lt;groupId&gt;javax&lt;/groupId&gt; &lt;artifactId&gt;javaee-api&lt;/artifactId&gt; 
			&lt;version&gt;7.0&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;
 
		&lt;!-- mybatis-plus begin --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.baomidou&lt;/groupId&gt;
			&lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt;
			&lt;version&gt;${mybatis-plus-boot-starter.version}&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- mybatis-plus end --&gt;
		&lt;!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;scope&gt;provided&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.seata&lt;/groupId&gt;
			&lt;artifactId&gt;seata-all&lt;/artifactId&gt;
			&lt;version&gt;0.9.0.1&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- &lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;dynamic-datasource-spring-boot-starter&lt;/artifactId&gt; 
			&lt;version&gt;2.5.4&lt;/version&gt; &lt;/dependency&gt; --&gt;
 
		&lt;!-- &lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-generator&lt;/artifactId&gt; 
			&lt;version&gt;3.1.0&lt;/version&gt; &lt;/dependency&gt; --&gt;
		&lt;!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.freemarker&lt;/groupId&gt;
			&lt;artifactId&gt;freemarker&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
			&lt;artifactId&gt;druid-spring-boot-starter&lt;/artifactId&gt;
			&lt;version&gt;1.1.20&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- 加上这个才能辨认到log4j2.yml文件 --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
			&lt;artifactId&gt;jackson-dataformat-yaml&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt; &lt;!-- 引入log4j2依赖 --&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
					&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
					&lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;!-- &lt;dependency&gt; &lt;groupId&gt;org.scala-lang&lt;/groupId&gt; &lt;artifactId&gt;scala-library&lt;/artifactId&gt; 
			&lt;version&gt;2.11.0&lt;/version&gt; &lt;/dependency&gt; --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

</code></pre>
<p>​	2.然后更改test-service的目录结构,删除zk的配置并更改application.yml文件,目录结构与代码:</p>
<pre><code class="language-yaml"><span class="hljs-attr">server:</span>
<span class="hljs-attr">  port:</span> <span class="hljs-number">38888</span>
<span class="hljs-attr">spring:</span>
<span class="hljs-attr">  application:</span> 
<span class="hljs-attr">      name:</span> <span class="hljs-string">test-service</span>
<span class="hljs-attr">  datasource:</span>
<span class="hljs-attr">    type:</span> <span class="hljs-string">com.alibaba.druid.pool.DruidDataSource</span>
<span class="hljs-attr">    url:</span> <span class="hljs-attr">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC</span>
<span class="hljs-attr">    driver-class-name:</span> <span class="hljs-string">com.mysql.cj.jdbc.Driver</span>
<span class="hljs-attr">    username:</span> <span class="hljs-string">root</span>
<span class="hljs-attr">    password:</span> <span class="hljs-number">123456</span>
<span class="hljs-attr">dubbo:</span>
<span class="hljs-attr">  protocol:</span>
<span class="hljs-attr">    loadbalance:</span> <span class="hljs-string">leastactive</span>
<span class="hljs-attr">    threadpool:</span> <span class="hljs-string">cached</span>
<span class="hljs-attr">  scan:</span>
<span class="hljs-attr">    base-packages:</span> <span class="hljs-string">org。test.service</span>
<span class="hljs-attr">  application:</span>
<span class="hljs-attr">    qos-enable:</span> <span class="hljs-literal">false</span>
<span class="hljs-attr">    name:</span> <span class="hljs-string">testserver</span>
<span class="hljs-attr">  registry:</span>
<span class="hljs-attr">    id:</span> <span class="hljs-string">my-registry</span>
<span class="hljs-attr">    address:</span>  <span class="hljs-attr">nacos://127.0.0.1:8848</span>
<span class="hljs-attr">mybatis-plus:</span>
<span class="hljs-attr">  mapper-locations:</span> <span class="hljs-attr">classpath:/mapper/*Mapper.xml</span>
<span class="hljs-attr">  typeAliasesPackage:</span> <span class="hljs-string">org.test.entity</span>
<span class="hljs-attr">  global-config:</span>
<span class="hljs-attr">    db-config:</span>
<span class="hljs-attr">      field-strategy:</span> <span class="hljs-string">not-empty</span>
<span class="hljs-attr">      id-type:</span> <span class="hljs-string">auto</span>
<span class="hljs-attr">      db-type:</span> <span class="hljs-string">mysql</span>
<span class="hljs-attr">  configuration:</span>
<span class="hljs-attr">    map-underscore-to-camel-case:</span> <span class="hljs-literal">true</span>
<span class="hljs-attr">    cache-enabled:</span> <span class="hljs-literal">true</span>      
<span class="hljs-attr">    auto-mapping-unknown-column-behavior:</span> <span class="hljs-string">none</span>
</code></pre>
<img src="/img/blog/20191202211833.png" alt="20191202211833" style="zoom:100%;" />
<p>​	3.再更改registry.conf文件,如果你的nacos是其它服务器,请改成对应都ip跟端口</p>
<pre><code class="language-java">registry {
  type = <span class="hljs-string">"nacos"</span>
  file {
    name = <span class="hljs-string">"file.conf"</span>
  }
   zk {
    cluster = <span class="hljs-string">"default"</span>
    serverAddr = <span class="hljs-string">"127.0.0.1:2181"</span>
    session.timeout = <span class="hljs-number">6000</span>
    connect.timeout = <span class="hljs-number">2000</span>
  }
    nacos {
    serverAddr = <span class="hljs-string">"localhost"</span>
    namespace = <span class="hljs-string">""</span>
    cluster = <span class="hljs-string">"default"</span>
  }
}
config {
  type = <span class="hljs-string">"nacos"</span>
  file {
    name = <span class="hljs-string">"file.conf"</span>
  }
  zk {
    serverAddr = <span class="hljs-string">"127.0.0.1:2181"</span>
    session.timeout = <span class="hljs-number">6000</span>
    connect.timeout = <span class="hljs-number">2000</span>
  }
    nacos {
    serverAddr = <span class="hljs-string">"localhost"</span>
    namespace = <span class="hljs-string">""</span>
    cluster = <span class="hljs-string">"default"</span>
  }
}
</code></pre>
<p>​	4.接着我们运行provideApplication</p>
<p><img src="/img/blog/20191202212000.png" alt="20191202212000"></p>
<p>启动成功啦,我们再去看seata的日志:</p>
<p><img src="/img/blog/20191202212028.png" alt="20191202212028"></p>
<p>成功了,这下我们一样,去修改test-client的内容,首先一样application.yml,把zk换成nacos,这里就不详细描述了,把test-service内的registry.conf,复制到client项目的resources中覆盖原来的registry.conf.</p>
<p>然后我们可以运行clientApplication:</p>
<p><img src="/img/blog/20191202212114.png" alt="20191202212114"></p>
<p>​	5.确认服务已经被发布并测试事务运行是否正常</p>
<p><img src="/img/blog/20191202212203.png" alt="20191202212203"></p>
<p>服务成功发布出来,也被成功消费了.这下我们再去swagger中去测试回滚是否一切正常,访问http://127.0.0.1:28888/swagger-ui.html</p>
<p><img src="/img/blog/20191202212240.png" alt="20191202212240"></p>
<p>恭喜你,看到这一定跟我一样成功了!</p>
<h1>总结</h1>
<p>关于nacos的使用跟seata的简单搭建已经完成了,更详细的内容希望希望大家访问以下地址阅读详细文档</p>
<p><a href="https://nacos.io/zh-cn/index.html">nacos官网</a></p>
<p><a href="http://dubbo.apache.org/en-us/">dubbo官网</a></p>
<p><a href="http://seata.io/zh-cn/">seata官网</a></p>
</section><footer class="footer-container"><div class="footer-body"><img src="/img/seata_logo_gray.png"/><p class="docsite-power">website powered by docsite</p><div class="cols-container"><div class="col col-12"><h3>愿景</h3><p>Seata 是一款阿里巴巴开源的分布式事务解决方案，致力于在微服务架构下提供高性能和简单易用的分布式事务服务。</p></div><div class="col col-6"><dl><dt>文档</dt><dd><a href="/zh-cn/docs/overview/what-is-seata.html" target="_self">Seata 是什么？</a></dd><dd><a href="/zh-cn/docs/user/quickstart.html" target="_self">快速开始</a></dd><dd><a href="https://github.com/seata/seata.github.io/issues/new" target="_self">报告文档问题</a></dd><dd><a href="https://github.com/seata/seata.github.io" target="_self">在Github上编辑此文档</a></dd></dl></div><div class="col col-6"><dl><dt>资源</dt><dd><a href="/zh-cn/blog/index.html" target="_self">博客</a></dd><dd><a href="/zh-cn/community/index.html" target="_self">社区</a></dd></dl></div></div><div class="copyright"><span>Copyright © 2019 Seata</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/blogDetail.js"></script>
	<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?104e73ef0c18b416b27abb23757ed8ee";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</body>
</html>
